﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Smith.ImageConverter
{
    public class BaseRename : IRename
    {
        ConvertType myTargetType;

        void OnTargetTypeChanged()
        {
            switch (TargetType)
            {
                case ConvertType.JPEG:
                    Extension = ".jpg";
                    break;
                case ConvertType.PNG:
                    Extension = ".png";
                    break;
                case ConvertType.BMP:
                    Extension = ".bmp";
                    break;
                default:
                    break;
            }
        }

        public ConvertType TargetType
        {
            get { return myTargetType; }
            set
            {
                if (myTargetType != value)
                {
                    myTargetType = value;
                    OnTargetTypeChanged();
                }
            }
        }

        public string Extension
        {
            get;
            private set;
        }

        public BaseRename()
        {
            OnTargetTypeChanged();
        }

        public virtual string GetNewName(string srcFile, string destDir)
        {
            if (destDir == null)
            {
                string d = GetDirectoryName(srcFile);
                string n = GetFileName(srcFile);
                return Path.Combine(d, n + Extension);
            }
            else
            {
                string n = GetFileName(srcFile);
                return Path.Combine(destDir, n + Extension);
            }
        }

        protected virtual string GetDirectoryName(string path)
        {
            return Path.GetDirectoryName(path);
        }

        protected virtual string GetFileName(string path)
        {
            return Path.GetFileNameWithoutExtension(path);
        }
    }
}
